var request = require('request');
// var serverHome = require('../config');
exports.nullDataReq=function(req,token,Dpath,Dpost,success){ 
    var options = {
        url: global.host+Dpath,
        method: Dpost,
        headers :{
            Cookie:token,
            ContentType :"application/json;charset=utf-8"
        }
    };
    function callback(error, response, data) {
        if (!error && response.statusCode == 200) {
            success(data);
        }
    }   
    request(options, callback);

}
exports.dataReq=function(req,token,data,Dpath,Dpost,success){ 
    var options = {
        url: global.host+Dpath,
        method: Dpost,
        json:true,
        body:data,
        headers :{
            Cookie:token,
            ContentType :"application/json;charset=utf-8"
        }
    };
    function callback(error, response, data) {
        if (!error && response.statusCode == 200) {
            success(data);
        }
    }  
    request(options, callback);
}

exports.importfileReq=function(req,token,data,Dpath,Dpost,success){ 
    //form表单格式
    var options = {
        url: global.host+Dpath,
        method: Dpost,
        body:data,
        headers :{
            Cookie:token
        }
    };
    function callback(error, response, data) {
        if (!error && response.statusCode == 200) {
            success(data);
        }
    }   
    request(options, callback);

}
exports.loginDataReq=function(req,data,Dpath,Dpost,success){ 
    var options = {
        url:global.host+Dpath,
        method: Dpost,
        headers :{
            ContentType :"application/json;charset=utf-8"
        },
        json:true,
        body:data
    };
    function callback(error, response, data) {
        if (!error && response.statusCode == 200) {
           var token = response.headers['set-cookie'][0];
           var token = token.substring(0,43);
            data.token = token;
            success(data);
        }
    }   
    request(options, callback);

}           
        
 exports.delEmptyValue=function(data){
    for(var i in data){
      if(data[i]=='' || data[i]==null || data[i]==undefined){
        delete data[i];
      }
    }
    return data;
 }   

exports.pageData = function(data){  //需要分页的情况
    var search={}
    for(var key in data){
        if(key !='pageSize' && key !='pageCurrent'){
            search[key]=data[key];
        }
    }

    if(JSON.stringify(search) !='{}'){
        return {'pageSize':parseInt(data.pageSize),'pageCurrent':data.pageCurrent/data.pageSize+1,'search':search}
    }else{
        return {'pageSize':parseInt(data.pageSize),'pageCurrent':data.pageCurrent/data.pageSize+1}
    }  
}




